package entity

import (
	"strconv"

	"gorm.io/gorm"
	"shqsoft.cn/pas3ui/base"
)

// BMTZJE
type BMTZJE struct {
	DPNM     string  `gorm:"column:DPNM"`     //部门
	EMNO     string  `gorm:"column:EMNO"`     //工号
	EMNM     string  `gorm:"column:EMNM"`     //姓名
	YWJX     float64 `gorm:"column:YWJX"`     //业务绩效
	YJJX     float64 `gorm:"column:YJJX"`     //业绩绩效
	EMID     int     `gorm:"column:EMID"`     //员工id
	DPID     int     `gorm:"column:DPID"`     //部门id
	PDID     int     `gorm:"column:PDID"`     //部门id
	FFJD     int     `gorm:"column:FFJD"`     //发放季度
	ISYYS    int     `gorm:"column:ISYYS"`    //营业室人员
	YWJXTZJE float64 `gorm:"column:YWJXTZJE"` //业务绩效调整金额
	YJJXTZJE float64 `gorm:"column:YJJXTZJE"` //业绩绩效调整金额
	TZYY     string  `gorm:"column:TZYY"`     //调整原因
	TZJECW   string  `gorm:"column:TZJECW"`   //调整原因错误
}

// TableName
func (BMTZJE) TableName() string {
	return "SH_JHCW_JXFF_YGJX"
}

// AfterFind 查询后触发器
func (c *BMTZJE) AfterFind(tx *gorm.DB) (err error) {
	err = can_import(tx, c.FFJD)
	if err != nil {
		return err
	}
	var quarter int
	month := c.FFJD % 100
	ffjd := strconv.Itoa(c.FFJD)
	year, _ := strconv.Atoi(ffjd[:4])
	if 1 <= month && month <= 3 {
		quarter = (year-1)*10 + 4
	} else if 4 <= month && month <= 6 {
		quarter = year*10 + 1
	} else if 7 <= month && month <= 9 {
		quarter = year*10 + 2
	} else {
		quarter = year*10 + 3
	}
	if base.AppConf.Database.Mysql != "" {
		sqlStr := `select IFNULL(ROUND(IFNULL(YWJX,0)/3,2),0) YWJX from "SH_JHCW_JXFF_YGJX" sjjy where "FFJD" =? and "EMNO"=?`
		tx.Raw(sqlStr, quarter, c.EMNO).Row().Scan(&c.YWJX)
	} else {
		sqlStr := `select round(COALESCE("YWJX",0)/3,2) YWJX from "SH_JHCW_JXFF_YGJX" sjjy where "FFJD" =?  and "EMNO"=?`
		tx.Raw(sqlStr, quarter, c.EMNO).Row().Scan(&c.YWJX)
	}
	sqlStr := `select "YWJXTZJE","YJJXTZJE","TZYY" from "SH_JHCW_JXFF_YGJX_TZJE" where "FFJD"=? and "EMNO"=?`
	tx.Raw(sqlStr, c.FFJD, c.EMNO).Row().Scan(&c.YWJXTZJE, &c.YJJXTZJE, &c.TZYY)
	return
}

// ShCompClieQygmEF 返回结构体
func BMTZJEEF() base.EntityFunc {
	return func() interface{} {
		entity := BMTZJE{}
		return &entity
	}
}

// ShCompClieQygmESF 返回结构体数组
func BMTZJEESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []BMTZJE
		return &entities
	}
}
func init() {
	base.DaoPoolObj.Put("BMTZJE", base.NewDefaultDao(BMTZJEEF(), BMTZJEESF(), nil, "my"))
}
